<template>
  <div>
    <el-card style="margin: 20px 0px">
      <CategorySelect
        @getCategoryID="getCategoryID"
        :show="scene != 0"
      ></CategorySelect
    ></el-card>
    <el-card>
      <div v-show="scene == 0">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="addSpu"
          :disabled="!category3ID"
          >添加Spu</el-button
        >
        <el-table style="width: 100%" border :data="records">
          <el-table-column
            type="index"
            label="序号"
            width="80px"
            align="center"
          ></el-table-column>
          <el-table-column label="Spu名称" prop="spuName"></el-table-column>
          <el-table-column label="Spu描述" prop="description"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="{ row, $index }">
              <hintButton
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSku(row)"
              ></hintButton>
              <hintButton
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="updateSpu(row)"
              ></hintButton>
              <hintButton
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看当前spu全部的sku列表"
                @click="handler(row)"
              ></hintButton>
              <el-popconfirm title="确定删除吗？" @confirm="deleteSpu(row)">
                <hintButton
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                  slot="reference"
                ></hintButton>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChange"
          @current-change="getSpuList"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout="prev, pager, next, jumper, ->, sizes,total"
          :total="total"
          style="text-align: center"
        >
        </el-pagination>
      </div>
      <SpuForm
        v-show="scene == 1"
        @changeScene="changeScene"
        ref="spu"
      ></SpuForm>
      <SkuForm v-show="scene == 2" ref="sku" @changeScenes="changeScenes">
      </SkuForm>
    </el-card>
    <!-- 
      1.before-close:关闭对话框前的回调函数
     -->
    <el-dialog
      :title="`${spu.spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
      :before-close="close"
    >
      <el-table :data="skuList" border v-loading="loading">
        <el-table-column
          label="名称"
          width="150"
          prop="skuName"
        ></el-table-column>
        <el-table-column
          label="价格"
          width="200"
          prop="price"
        ></el-table-column>
        <el-table-column label="重量" prop="weight"></el-table-column>
        <el-table-column prop="" label="默认图片">
          <template slot-scope="{ row, $index }">
            <img
              :src="row.skuDefaultImg"
              alt=""
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from "@/views/product/Spu/SpuForm";
import SkuForm from "@/views/product/Spu/SkuForm";
export default {
  name: "Spu",
  data() {
    return {
      loading: true,
      spu: {},
      dialogTableVisible: false,
      // 0代表展示spu列表数据,1表示添加spu|修改spu,2代表添加sku
      scene: 0,
      page: 1,
      limit: 3,
      category1ID: "",
      category2ID: "",
      category3ID: "",
      total: 0,
      // 存储Spu列表数据
      records: [],
      skuList: [],
    };
  },
  components: { SpuForm, SkuForm },
  methods: {
    close(done) {
      // 解决loading的值变为false后不会再变为true的问题
      this.loading = true;
      // 解决数据回显
      this.skuList = [];
      // 关闭对话框
      done();
    },
    async handler(spu) {
      this.dialogTableVisible = true;
      this.spu = row;
      let result = await this.$API.Sku.reqSkuList(spu.id);
      if (result.code == 200) {
        this.skuList = result.data;
        this.loading = false;
      }
    },
    changeScenes(scene) {
      this.scene = scene;
    },
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDelete(row.id);
      if (result.code == 200) {
        this.$message({ type: "success", message: "删除成功" });
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
      }
    },
    changeScene({ scene, flag }) {
      this.scene = scene;
      if (flag == "修改") {
        this.getSpuList(this.page);
      } else {
        this.getSpuList();
      }
    },
    updateSpu(row) {
      this.scene = 1;
      this.$refs.spu.initSpuData(row);
    },
    addSpu() {
      this.scene = 1;
      this.$refs.spu.addSpuData(this.category3ID);
    },
    sizeChange(limit) {
      this.limit = limit;
      this.getSpuList();
    },
    getCategoryID({ categoryID, level }) {
      // 父组件中接收相应的ID从而获取到数据
      if (level == 1) {
        this.category1ID = categoryID;
        this.category2ID = "";
        this.category3ID = "";
      } else if (level == 2) {
        this.category2ID = categoryID;
        this.category3ID = "";
      } else {
        this.category3ID = categoryID;
        this.getSpuList();
      }
    },
    async getSpuList(pager = 1) {
      this.page = pager;
      const { page, limit, category3ID } = this;
      let result = await this.$API.Spu.reqSpuList(page, limit, category3ID);
      if (result.code == 200) {
        this.total = result.data.total;
        this.records = result.data.records;
        return "ok";
      } else {
        return Promise.reject(new Error("failure"));
      }
    },
    addSku(row) {
      this.scene = 2;
      this.$refs.sku.getData(this.category1ID, this.category2ID, row);
    },
  },
};
</script>

<style scoped>
</style>
